/*
** ###################################################################

** ###################################################################
*/
#ifndef __BASEREF_H
#define __BASEREF_H

typedef unsigned char   Byte;
typedef unsigned int      Word;
typedef unsigned long   Longword;
typedef   signed char    SByte;
typedef   signed int       SWord;
typedef   signed long    SLongword;

typedef  void*           Void_ptr;
/* =========================================================================
                      Interrupt vector table translation
   ========================================================================= */

typedef void (*Voidf_ptr)(void);
typedef Byte (*Bytef_ptr)(void);

/* =========================================================================
                                General macros
   ========================================================================= */
#ifndef NULL
#define NULL                (void *)0
#endif
#ifndef ON
#define ON                  0x01
#endif
#ifndef OFF
#define OFF                 0x00
#endif
#ifndef TRUE
#define TRUE                0x01
#endif
#ifndef FALSE
#define FALSE               0x00
#endif
#ifndef YES
  #define YES                 0x01
#endif
#ifndef NO
  #define NO                  0x00
#endif
/* Bit mask */
#define B(n)            ((Byte)(0x01<<n))

#define __p(p,b) p
#define __b(p,b) b
#define SET(x) (__p(x)|=__b(x))       //set flag
#define CLR(x) (__p(x)&=~(__b(x)))    //clr flag
#define TOG(x) (__p(x)^=__b(x))       //toggle flag 
#define VAL(x) ((Byte) (__p(x)&(__b(x))))     //test bit value
#define BIT(x)  (__b(x))     // bit value

#define OUT(x) (__p(x)|=__b(x))       //set flag
#define IN(x) (__p(x)&=~(__b(x)))    //clr flag


#define BYTE(b)		((Byte)(b))
#define WORD(w)		((Word)(w))

/* Absolute value of x */
#define ABS(x)              ( ((x)>=0)*(x) + ((x)<0)*(-(x)) )
/* Return: 0 if x==0, -1 if x<0, +1 if x>0 */
#define SIGN3(x)            ( ((x)>0)*1 + ((x)<0)*(-1) )
/* Round x to integer type t toward zero. Standard C integer cast operation. Returns the whole number of x */
#define TRUNC(t,x)          ((t)(x))
/* Round x to integer type t to nearest integer. N-0.5 <= x < N+0.5 */
#define ROUND(t,x)          ( (t)(((x)>=0)*((x)+0.5) + ((x)<0)*((x)-0.5)) )
/* Round x to integer type t toward infinity */
#define BROADEN(t,x)        ( TRUNC(t,x) + (((x)-TRUNC(t,x))!=0)*SIGN3(x) )
/* Round x to integer type t toward plus infinity. Return nearest integer greater than or equal to x: N >= x */
#define CEIL(t,x)           ( TRUNC(t,x) + ((x)>0)*(((x)-TRUNC(t,x))!=0) )
/* Round x to integer type t toward minus infinity. Return nearest integer less than or equal to x: N <= x */
#define FLOOR(t,x)          ( TRUNC(t,x) - ((x)<0)*(((x)-TRUNC(t,x))!=0) )

/* Specific operations (DIV and MUL) with integer casting */
#define ROUND_DIV(t,a,b)    ROUND(t,(a)/(b))
#define TRUNC_DIV(t,a,b)    TRUNC(t,(a)/(b))
#define CEIL_DIV(t,a,b)     CEIL(t,(a)/(b))

#define ROUND_MUL(t,a,b)    ROUND(t,(a)*(b))


/* Number of elements (not byte) of an array */
#define elemof(v)             (sizeof(v)/sizeof(v[0]))

/* As defined in stddef.h; styp = struct type, mbr = name of its member */
//#define offsetof(styp, mbr)   ((unsigned short int)&(((styp *)0)->mbr))

/* The word from b */
#define wordFrom(b)           (*(Word *)&(b))
/* The byte at byte offset i of b (b can be of any type size) */
#define byteOf(b,i)           (((Byte *)&(b))[(i)])

/* To encapsulate code containing more than 1 instruction in safe and compatible mode */
#define ENCAPS(code)          do { code } while (0)

/* Do nothing (no instruction are generated) */
#define NOTHING()             (void)0

/* -------------------------------------------------------------------------
                         Useful compile-time operators
   ------------------------------------------------------------------------- */

/* Choose the maximum between a and b */
#define MAX(a,b)            ((a)>=(b) ? (a) : (b))
/* Choose the minimum between a and b */
#define MIN(a,b)            ((a)<=(b) ? (a) : (b))

/* cnt is a variable of type Byte or Word or Longword, this returns 0xff, 0xffff or 0xffffffff */
#define MAX_UCOUNT(cnt)     ( (0xff*(sizeof(cnt)==1)) + (0xffff*(sizeof(cnt)==2)) + (0xffffffff*(sizeof(cnt)==4)))

/* cnt is a variable of type SByte or SWord or SLongword, this returns 0x7f=127, 0x7fff=32767 or 0x7fffffff=2147483647 */
#define MAX_SCOUNT(cnt)     ( (0x7f*(sizeof(cnt)==1)) + (0x7fff*(sizeof(cnt)==2)) + (0x7fffffff*(sizeof(cnt)==4)))

/* cnt is a variable of type SByte or SWord or SLongword, this returns 0x80=-128, 0x8000=-32768 or 0x80000000=-2147483648 */
#define MIN_SCOUNT(cnt)     ( (0x80*(sizeof(cnt)==1)) + (0x8000*(sizeof(cnt)==2)) + (0x80000000*(sizeof(cnt)==4)))

/* -------------------------------------------------------------------------
                             Integer Manipulation
   ------------------------------------------------------------------------- */



/* =========================================================================
                      Interrupt vector table translation
   ========================================================================= */

#define EN_INT()    { asm CLI;}
#define DIS_INT()   { asm SEI;}

#endif

